package top.guxx.mall.job;

import com.alipay.api.AlipayClient;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.guxx.mall.service.IOrderService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author：Guxx
 * @Description：
 * @Date：2025/2/23 1:12
 * @Filename：NoPayNotifyOrderJob
 */
@Slf4j
@Component
public class NoPayNotifyOrderJob {

    @Resource
    private IOrderService orderService;

    @Resource
    private AlipayClient alipayClient;

    @Scheduled(cron = "0/3 * * * * ?")
    public void exec(){
        try {
            List<String> orders = orderService.queryTimeoutCloseOrderList();
            if (orders == null || orders.isEmpty()) return;
            for (String orderId : orders) {
                AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
                AlipayTradeQueryModel bizModel = new AlipayTradeQueryModel();
                bizModel.setTradeNo(orderId);
                request.setBizModel(bizModel);
                AlipayTradeQueryResponse response = alipayClient.execute(request);
                if ("10000".equals(response.getCode())){
                    orderService.changeOrderPaySuccess(orderId);
                }
            }
        }catch (Exception e){
            log.error("检测未接收到或未正确处理的支付回调通知失败", e);
        }
    }
}
